
TOOL.Category		= "Render"
TOOL.Name			= "#Material"
TOOL.Command		= nil
TOOL.ConfigName		= nil


TOOL.ClientConVar[ "override" ] = "debug/env_cubemap_model"

function TOOL:LeftClick( trace )

	if !( trace.Entity &&			// Hit an entity
	      trace.Entity:IsValid() && 	// And the entity is valid
	      trace.Entity:EntIndex() != 0	// And isn't worldspawn
	    ) then return end

	local mat = self:GetClientInfo( "override" )
	SetMaterial( self:GetOwner(), trace.Entity, mat )
	return true

end

function SetMaterial( pl, Ent, mat )

	if (!mat) then return end
	if (!Ent || !Ent:IsValid()) then return end

	Ent:SetMaterial( mat )
	Ent:GetTable().material = {mat = mat}

	return true

end
duplicator.RegisterEntityModifier( "material", SetMaterial, "mat" )

function TOOL:RightClick( trace )

	if !( trace.Entity &&			// Hit an entity
	      trace.Entity:IsValid() && 	// And the entity is valid
	      trace.Entity:EntIndex() != 0	// And isn't worldspawn
	    ) then return end

	trace.Entity:SetMaterial( "" )
	trace.Entity:GetTable().material = nil
	return true

end
